import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/home/HomeView.vue";
// import MainView from '../views/MainView.vue'
import store from "@/store";

import { loginToken } from "../assets/js/index";
Vue.use(VueRouter);

const roles = ["admin", "operator", "technologist"];

const routes = [
  // 登录页路由
  {
    path: "/login",
    name: "Login",
    component: () => import("../components/Login/index.vue"),
  },
  {
    path: "/",
    redirect: "/home",
  },

  // 首页路由
  {
    path: "/home",
    name: "home",
    component: HomeView,
  },
  //{ path: "/main", name: "main", component: MainView },
  //设备维护页面
  {
    path: "/maintain/:id",
    name: "maintain",
    component: () => import("../views/maintain/index.vue"),
    meta: {},
  },
  // 设置页面
  {
    path: "/settings",
    name: "settings",
    component: () => import("../views/SetUpindex/index.vue"),
    redirect: "/settings/UserSettings",
    meta: { roles: ["admin", "technologist"] },
    children: [
      // 通讯设置模块
      {
        path: "/settings/CommunicationSettings",
        name: "CommunicationSettings",
        component: () =>
          import("../views/SetUpindex/CommunicationSettings/index.vue"),
        meta: { roles: ["admin"] },
      },
      // 用户设置模块
      {
        path: "/settings/UserSettings",
        name: "UserSettings",
        component: () => import("../views/SetUpindex/UserSettings/index.vue"),
        meta: { roles: ["admin", "technologist"] },
      },
      // 基本信息设置模块
      {
        path: "/settings/InformationSettings",
        name: "InformationSettings",
        component: () =>
          import("../views/SetUpindex/InformationSettings/index.vue"),
        meta: { roles: ["admin"] },
      },
      //维护保养组件设置模块
      {
        path: "/settings/MaintainComponentSettings",
        name: "MaintainComponentSettings",
        component: () =>
          import("../views/SetUpindex/MaintainComponentSettings/index.vue"),
        meta: { roles: ["admin"] },
      },
      //  故障信息设置模块
      {
        path: "/settings/FaultMessageSettings",
        name: "FaultMessageSettings",
        component: () =>
          import("../views/SetUpindex/FaultMessageSettings/index.vue"),
        meta: { roles: ["admin"] },
      },
      // 故障解决方案模块
      {
        path: "/settings/TroubleshootingSettings",
        name: "TroubleshootingSettings",
        component: () =>
          import("../views/SetUpindex/TroubleshootingSettings/index.vue"),
        meta: { roles: ["admin"] },
      },
      // 班次配置模块

      {
        path: "/settings/ClassOrderSettings",
        name: "ClassOrderSettings",
        component: () =>
          import("../views/SetUpindex/ClassOrderSettings/index.vue"),
        meta: { roles: ["admin"] },
      },
    ],
  },
  //数据统计页面
  {
    path: "/statistics",
    name: "statistics",
    component: () => import("../views/datastatistics/index.vue"),
    redirect: "/statistics/FaultMaintenance",
    meta: { roles: ["admin", "technologist"] },
    children: [

      {
        path: "/statistics/Clockrecords",
        name: "Clockrecords",
        component: () =>
          import("../views/datastatistics/Clockrecords/index.vue"),
        meta: { roles: ["admin"] },
      },
      {
        path: "/statistics/FaultMaintenance",
        name: "FaultMaintenance",
        component: () =>
          import("../views/datastatistics/FaultMaintenance/index.vue"),
        meta: { roles: ["admin"] },
      },
      // 统计每天总产量
      {
        path: "/statistics/ProductionStatistics",
        name: "ProductionStatistics",
        component: () =>
          import("../views/datastatistics/ProductionStatistics/index.vue"),
        meta: { roles: ["admin"] },
      },
      //故障报警记录
      {
        path: "/statistics/MesRecordExceptFaultAlarmRecord",
        name: "MesRecordExceptFaultAlarmRecord",
        component: () =>
          import("..//views/datastatistics/MesRecordExceptfaultAlarmRecord/index.vue"),
        meta: { roles: ["admin"] },
      },
       //保养记录
       {
        path: "/statistics/maintenanceLog",
        name: "maintenanceLog",
        component: () =>
          import("..//views/datastatistics/MaintenanceLog/index.vue"),
        meta: { roles: ["admin"] },
      },


    ],
  },
];

const router = new VueRouter({
  mode: "hash", //electron+vue项目中router不能用history模式
  //base: process.env.BASE_URL,
  routes,
});

//todo
// router.beforeEach((to, from, next) => {
//   let token = loginToken();
//   if (to.path == "/login") {
//     next();
//   } else {
//     if (token !== "") {
//       console.log(1111);
//       next();
//     } else {
//       next("/login");
//     }
//   }
// });


const whitelist = ["/", "/login", "/home", "/main"];

router.beforeEach((to, from, next) => {


  let token = store.getters.token;
  let role = store.getters.role; //?
  let isLogin = !!token;
  if (whitelist.indexOf(to.path) !== -1) {
    next(); // release whitelist
    return;
  }
  if (!to.meta.roles) {
    next();
    return;
  }
  if (!isLogin) {
    next();
  } else {
    if (to.path === "/login") {
      //removeToken
      store.commit("Userinfo/LOGOUT");
      next();
    } else if (to.meta.roles && to.meta.roles.includes(role)) {
      next();
    }
  }
});
export default router;
